package model.displayer;

import controller.MainController;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import model.Point;
import model.utils.Utils;

public class RectangleDisplayer extends ShapeDisplayerJFxImpl
{
    public static final double DEFAULT_HEIGHT = 40.0;
    public static final double DEFAULT_WIDTH = 20.0;
    public static final Point DEFAULT_LEFT_TOP = new Point(10,10);

    private Rectangle rectangle;

    RectangleDisplayer(Pane canvas, MainController controller)
    {
        super(canvas,controller);
        this.shape = new Rectangle();
        shape.setStroke(DEFAULT_STROKE);
        shape.setFill(DEFAULT_FILL);
        shape.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        shape.setOnMouseClicked((MouseEvent e)->controller.handleShapeClicked(this));

        this.rectangle=(Rectangle)this.shape;
        this.rectangle.setX(DEFAULT_LEFT_TOP.getX());
        this.rectangle.setY(DEFAULT_LEFT_TOP.getY());
        this.rectangle.setWidth(DEFAULT_WIDTH);
        this.rectangle.setHeight(DEFAULT_HEIGHT);
    }

    @Override
    protected void resize(double multi)
    {
        Point leftTop = new Point(this.rectangle.getX() , this.rectangle.getY());
        Point rightBottom = new Point(this.rectangle.getX() + this.rectangle.getWidth(),
                                        this.rectangle.getY() + this.rectangle.getHeight());
        Point center = Utils.calcMidPoint(leftTop,rightBottom);

        double height = rectangle.getHeight();
        double width = rectangle.getWidth();

        height *= (1+multi);
        width *= (1+multi);

        Point newLeftTop = new Point(center.getX() - width / 2, center.getY() - height / 2);
        Point newRightBottom = new Point(center.getX() + width / 2, center.getY() + height/2);

        setLocation(newLeftTop,newRightBottom);
    }

    @Override
    public void setLocation(Point start,Point end)
    {
        double leftTopX = Math.min(start.getX(),end.getX());
        double leftTopY = Math.min(start.getY(),end.getY());
        double width = Math.abs(end.getX() - start.getX());
        double height = Math.abs(end.getY() - start.getY());
        this.rectangle.setX(leftTopX);
        this.rectangle.setY(leftTopY);
        this.rectangle.setWidth(width);
        this.rectangle.setHeight(height);
    }

    @Override
    public ShapeDisplayer clone()
    {
        RectangleDisplayer copy = new RectangleDisplayer(this.canvas,this.controller);
        copy.rectangle.setX(this.rectangle.getX());
        copy.rectangle.setY(this.rectangle.getY());
        copy.rectangle.setHeight(this.rectangle.getHeight());
        copy.rectangle.setWidth(this.rectangle.getWidth());
        copy.rectangle.setStroke(this.rectangle.getStroke());
        copy.rectangle.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        return copy;
    }

    @Override
    protected Point getCenter()
    {
        Point leftTop = new Point(this.rectangle.getX() , this.rectangle.getY());
        Point rightBottom = new Point(this.rectangle.getX() + this.rectangle.getWidth(),
                this.rectangle.getY() + this.rectangle.getHeight());

        Point center = Utils.calcMidPoint(leftTop,rightBottom);
        return center;
    }

    @Override
    protected void setCenter(Point newCenter)
    {
        Point center = getCenter();
        double XMove = newCenter.getX()-center.getX();
        double YMove = newCenter.getY()-center.getY();

        this.rectangle.setX(this.rectangle.getX() + XMove);
        this.rectangle.setY(this.rectangle.getY() + YMove);
    }
}
